package com.view;
import android.app.ActionBar;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.bean.User;
import com.firewarning.R;
import com.service.UserService;
import com.util.DatabaseManger;

public class LoginActivity extends BaseActivity {
   private EditText editPhone;
    private EditText editPassword;
    private TextView toRegister;
    private Button login;
    private EventHandler handler=new EventHandler();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        android.support.v7.app.ActionBar actionBar = getSupportActionBar();
        if(actionBar!=null) {
            actionBar.hide();
        }
        editPassword=(EditText)this.findViewById(R.id.login_password);
        editPhone=(EditText)this.findViewById(R.id.login_phone);
        login=(Button)findViewById(R.id.login_login);
        login.setOnClickListener(handler);
        toRegister=(TextView)this.findViewById(R.id.login_register);
       toRegister.setOnClickListener(handler);
    }
    class EventHandler implements View.OnClickListener{

        @Override
        public void onClick(View view) {
            switch (view.getId()){
                case R.id.login_login:
                    String phone = editPhone.getText().toString().trim();
                    String password = editPassword.getText().toString().trim();
                    User user=UserService.checkUser(LoginActivity.this,phone,password);
                    if(user!=null) {
                        UserService.setSession(user,LoginActivity.this);
                        LoginActivity.this.startActivity(new Intent(LoginActivity.this, MainActivity.class));
                    }
                    else{
                        Toast.makeText(LoginActivity.this,"电话或密码错误",Toast.LENGTH_SHORT).show();
                    }break;
                case R.id.login_register:
                    LoginActivity.this.startActivity(new Intent(LoginActivity.this,RegisterActivity.class));break;


            }
        }
    }
}